﻿package com._public._ui
{
	import com._public._transitions.TweenLite;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author
	 *
	 * 例子:
	 * import ScList;
		var gl:ScList = new ScList(120,200);
		addChild(gl);
		gl.x = gl.y = 100;
		gl.textFormat = new TextFormat("12",null,0xff6900);
		gl.addItem({label:"xx"});
		gl.addEventListener(Event.SELECT, selFun)
		
		function selFun(e:Event):void{trace(list.selectedItem.label)}
	 *
	 */
	public class ScList extends Sprite
	{
		private var _width:Number;
		private var _height:Number;
		private var _mask:Shape;
		private var _distance:Number = 10;
		private var _itemAry:Array;
		private var _num:Number = 0;
		private var _container:Sprite;
		private var _itemColor:uint = 0xf0f0f0;
		private var _textFormat:TextFormat;
		private var _selectedItem:Object;
		private var _selectedItemArray:Array;
		private var _itemData:Array;
		private var _selectedColor:uint = 0x97BDD9
		private var _selelctedData:Object;
		private var _selectedArray:Array;
		/**
		 * 使用选中标识
		 */
		public var useSelect:Boolean=true;
		public function ScList(wid:Number,hei:Number)
		{
			_textFormat = new TextFormat;
			_container = new Sprite;
			_itemAry = [];
			_itemData = [];
			_selectedArray = [];
			_selectedItemArray = [];
			_width = wid;
			_height = hei;
			_mask = new Shape;
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0, _distance, _width - _distance * 2, _height - _distance * 2);
			_mask.graphics.endFill();
			addChild(_mask);
			addChild(_container);
			_container.x = 0;
			_container.y = _distance;
			_container.mask = _mask;
			_container.addEventListener(MouseEvent.CLICK, itemClick);
			//this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
		}
		private function onOver(evt:MouseEvent):void
		{
			if (_container.height > _height - _distance * 2)
			{
				this.addEventListener(MouseEvent.MOUSE_OUT, onOut);
				this.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			}
		}

		private function onOut(evt:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}

		private function onMove(evt:MouseEvent):void
		{
			var my:Number = this.mouseY;
			if (my > _distance && my < _height - _distance)
			{
				TweenLite.to(_container, .2, { y:-((_container.height-_mask.height) / _mask.height) * (my - _distance)+_distance } );
			}
		}
		
		public function get list():Array
		{
			return _itemAry;
		}
		public function get listData():Array
		{
			return _itemData;
		}
		public function get selectedItem():Object
		{
			return _selectedItem;
		}
		private function itemClick(evt:MouseEvent):void
		{
			var key:int = _itemAry.indexOf(evt.target);
//			if (_selectedItem != null)
//			{
//				changeBg(_selectedItem as Sprite, _itemColor)
//			}
			//-----是否已选中-----
			var isSelected:Boolean = false;
			if(_selectedItemArray.indexOf(_itemAry[key])>-1)
			{
				_selectedItemArray.splice(_selectedItemArray.indexOf(_itemAry[key]),1);
				_selectedArray.splice(_selectedArray.indexOf(_itemData[key]),1);
				isSelected = true;
			}
//			for(var i:int=0;i++;i<_selectedItemArray.length)
//			{
//				changeBg(_selectedItemArray[i] as Sprite, _itemColor);
//			}
			_selectedItem = _itemAry[key];
			_selelctedData = _itemData[key];
			
			if(useSelect){
			//----chf-----
			if(!isSelected)
			{
				if(_selectedArray.indexOf(_itemData[key])<0) _selectedArray.push(_itemData[key]);
				
				if(_selectedItemArray.indexOf(_itemAry[key])<0) _selectedItemArray.push(_itemAry[key]);
			}
			changeBg(_selectedItem as Sprite, _selectedColor);
			}
			this.dispatchEvent(new Event(Event.SELECT));
		}
		public function get seletedData():Object
		{
			return _selelctedData;
		}
		public function get selectedArray():Array
		{
			return _selectedArray;
		}
		public function set itemColor(value:uint):void
		{
			_itemColor = value;
		}
		public function refresh():void
		{
			_num = 0;
			while (_container.numChildren)
			{
				_container.removeChildAt(0);
			}
			for (var i:int = 0, len:int = _itemAry.length; i < len; i += 1 )
			{
				addItem(_itemData[i]);
			}
		}
		public function addItem(object:Object):void
		{
			var item:Sprite = createItem(object.label);
			_container.addChild(item);
			item.y = _num;
			_itemData.push(object);
			_itemAry.push(item);
			_num += item.height + 1;
			item.addEventListener(MouseEvent.MOUSE_OVER, itemOver);
		}
		public function clear():void
		{
			_itemAry = [];
			_itemData = [];
			_selectedItem = null;
			//----------chf----
			_selectedItemArray = [];
			_selectedArray = [];
			//----
			_container.y = _distance;
			this.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			refresh();
		}
		private function itemOver(evt:MouseEvent):void
		{
			//if (evt.currentTarget != _selectedItem)
			if(_selectedItemArray.indexOf(evt.currentTarget)<0)
			{
				changeBg(evt.currentTarget as Sprite, 0xdddddd)
			}
			evt.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, itemOut);
			evt.currentTarget.addEventListener(MouseEvent.MOUSE_DOWN, itemDown);
			evt.currentTarget.addEventListener(MouseEvent.MOUSE_UP, itemUp);
		}
		private function itemDown(evt:MouseEvent):void
		{
			changeBg(evt.currentTarget as Sprite, 0xcccccc)
			
		}
		private function itemUp(evt:MouseEvent):void
		{
			changeBg(evt.currentTarget as Sprite, 0xdddddd)
		}
		private function itemOut(evt:MouseEvent):void
		{
			//if (evt.currentTarget != _selectedItem)
			if(_selectedItemArray.indexOf(evt.currentTarget)<0)
			{
				changeBg(evt.currentTarget as Sprite, _itemColor)
			}
			evt.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, itemOut);
			evt.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, itemDown);
			evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, itemUp);
		}
		private function changeBg(spr:Sprite,col:uint):void
		{
			var wid:Number = spr.width;
			var hei:Number = spr.height;
			spr.graphics.clear()
			spr.graphics.beginFill(col);
			spr.graphics.drawRect(0, 0, wid, hei)
			spr.graphics.endFill()
		}
		public function set backgroundColor(value:uint):void
		{
			this.graphics.clear();
			this.graphics.beginFill(value);
			this.graphics.drawRect(0, 0, _width, _height);
			this.graphics.endFill();
		}
		public function set textFormat(value:TextFormat):void
		{
			_textFormat = value;
		}
		private function createItem(str:String):Sprite
		{
			var txt:TextField = new TextField;
			var sp:Sprite = new Sprite;
			txt.mouseEnabled = false;
			txt.autoSize = "left";
			txt.text = str;
			txt.width = _width - _distance * 2;
			txt.wordWrap = true;
			txt.setTextFormat(_textFormat);
			txt.y = (_height - _distance * 2)/2 - txt.height/2
			sp.addChild(txt);
			sp.graphics.beginFill(_itemColor);
			sp.graphics.drawRect(0, 0, _width - _distance * 2, _height - _distance * 2);
			sp.graphics.endFill();
			return sp;
		}
		
	}
	
}
